/**
 * 
 */
package checkip;

import it.unipr.ce.dsg.s2p.message.BasicMessage;
import it.unipr.ce.dsg.s2p.message.Payload;
import it.unipr.ce.dsg.s2p.peer.Peer;
import it.unipr.ce.dsg.s2p.sip.Address;
import it.unipr.ce.dsg.s2p.sip.sbc.GatewayPeer;

import java.util.GregorianCalendar;
import java.util.HashMap;

import org.unisannio.util.Converter;
import org.unisannio.util.ServiceInfo;
import org.zoolu.net.IpAddress;
import org.zoolu.sip.message.Message;
import org.zoolu.sip.provider.SipProvider;

/**
 * @author Fabio Melillo
 *
 */
public class SimpleTestPeer extends Peer {
	
	
	private String peerName="alessandro";
	private String port="3333";
	private String superPeer="superPeer@192.168.65.17:2222";
	
	
	public SimpleTestPeer(String pathConfig, String key, String peerName,
			int peerPort) {
		super(pathConfig, key, peerName, peerPort);
		System.out.println(sipProvider);
	}

	/* (non-Javadoc)
	 * @see it.unipr.ce.dsg.s2p.sip.Node#onDeliveryMsgFailure(java.lang.String, it.unipr.ce.dsg.s2p.sip.Address, java.lang.String)
	 */
	@Override
	protected void onDeliveryMsgFailure(String arg0, Address arg1, String arg2) {
		System.out.println("MSG Failure");
		System.out.println(arg1.toString());
		System.out.println(arg2);
		System.exit(0);
	}

	/* (non-Javadoc)
	 * @see it.unipr.ce.dsg.s2p.sip.Node#onDeliveryMsgSuccess(java.lang.String, it.unipr.ce.dsg.s2p.sip.Address, java.lang.String)
	 */
	@Override
	protected void onDeliveryMsgSuccess(String arg0, Address arg1, String arg2) {
		System.out.println("MSG Success");
		System.out.println(arg0);
		System.out.println(arg1.toString());
		System.out.println(arg2);
	}
	
	
	public void setSbc(String sbcAddress){
		nodeConfig.sbc=sbcAddress;
	}
	
	@Override
	protected void onReceivedMsg(String peerMsg, Address sender,
			String contentType) {
		super.onReceivedMsg(peerMsg, sender, contentType);
		System.out.println("\n\n\nON RECEIVED MSG");
		System.out.println(peerMsg);
		System.out.println(sender.toString());
	}
	
	@Override
	public void onReceivedMessage(SipProvider sipPvd, Message sipMsg) {
		super.onReceivedMessage(sipPvd, sipMsg);
		System.out.println("\n\n\nON RECEIVED MESSAGE");
		System.out.println(sipMsg.toString());
		System.out.println("[LETTO DAI PARAMETRI] remote address="+sipMsg.getRemoteAddress().toString());
		System.out.println("[LETTO DAI PARAMETRI] remote port="+sipMsg.getRemotePort());
		String sipAddress=sipMsg.getFromHeader().getNameAddress().getAddress().toString();
		String name=sipAddress.substring(4, sipAddress.indexOf("@"));
		System.out.println("[LETTO DAI PARAMETRI] remote name="+name);
		
		
		
		
//		Address myAddress=this.getAddress();
//		String toContactMe=this.peerDescriptor.getContactAddress();
//		System.out.println("My Address= "+myAddress);
//		System.out.println("To ContactAddress= "+toContactMe);
		
	}
	
	@Override
	protected void onReceivedSBCContactAddress(Address cAddress) {
		// TODO Auto-generated method stub
		System.out.println("SBC Contact RCVD");
		super.onReceivedSBCContactAddress(cAddress);
		System.out.println(cAddress.toString());
		ServiceInfo info=new ServiceInfo();
		info.addDescriptor("uri.di.prova", null, null, ServiceInfo.BROADCAST_RECEIVER);
		info.setMac(peerName);
		this.peerDescriptor.setAddress(getAddressPrivate().toString());
		info.setIp(getAddressPrivate().getHost());
		info.setPort(String.valueOf(getAddressPrivate().getPort()));
		BasicMessage basicMessage =new Converter().marshal(info, Converter.PUBLISH);
		send(new Address("superPeer", "192.168.65.17", 2222), basicMessage);
		
		
		
//		send(new Address("superPeer", "192.168.65.17", 2222),conAddress, basicMessage);
		
//		Address myAddress=this.getAddress();
//		String toContactMe=this.peerDescriptor.getContactAddress();
//		System.out.println("My Address= "+myAddress);
//		System.out.println("To ContactAddress= "+toContactMe);
	}
	
	
	//questo riceve il messaggio NAT-ON
	//chiama request public address
	@Override
	protected void onReceivedSBCMsg(String SBCMsg) {
		// TODO Auto-generated method stub
//		System.out.println("SBC MSG RCVD");
		super.onReceivedSBCMsg(SBCMsg);
		System.out.println(SBCMsg);
//		Address myAddress=this.getAddress();
//		String toContactMe=this.peerDescriptor.getContactAddress();
//		System.out.println("My Address= "+myAddress);
//		System.out.println("To ContactAddress= "+toContactMe);
	}
	
	private Address getAddressPrivate(){
		if(peerDescriptor.getContactAddress()==null)
			return new Address(peerName,IpAddress.getLocalHostAddress().toString(),3333);
		else{
			String contactAdd=peerDescriptor.getContactAddress();
			int at = contactAdd.indexOf("@");
			int index = contactAdd.indexOf(":");
			String name = contactAdd.substring(0, at);
			String ip = contactAdd.substring(at + 1, index);
			String contactPort = contactAdd.substring(index + 1);
			Address conAddress=new Address(name, ip, Integer.parseInt(contactPort));
			return conAddress;
		}
			
	}

}
